//
//  AtWllRunView.m
//  DefendHealth
//
//  Created by UNOHACHA on 17/3/3.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "AtWllRunView.h"

#import "Header.h"



@interface AtWllRunView ()
{
 
    UIButton * _enB;
    UIButton * _beB;
    

    NSInteger hour,min,S;
    
    BOOL isRun;
   
}

@end



@implementation AtWllRunView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self RunViewShard];
        
        isRun = YES;
    }
    
    return self;
    
}
#pragma mark -- 界面搭建

- (void)RunViewShard {
        
        UIImageView * runView  = [ReduceCode creatImageVeiwWithFrame:CGRectMake(0, 0, self.width, self.height) imageName:@"grayRun" isCorner:NO];
        runView.userInteractionEnabled = YES;
        [self addSubview:runView];
        
        
        UILabel * runTimerL = [ReduceCode creatLabelWithFrame:CGRectMake(kScreenWidth/4, 50, kScreenWidth/2, 35) title:@"00:00:00" titleColor:UIColorFromRGB(0x333333) font:35 textAlignment:NSTextAlignmentCenter];
        _runTimerL = runTimerL;
        runTimerL.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:35];
        runTimerL.centerX = runView.centerX;
        
        [runView addSubview:runTimerL];
        
        
        UILabel * matchSpeed = [ReduceCode creatLabelWithFrame:CGRectMake(0, kMaxY(runTimerL.frame)-20, kScreenWidth/4, 20) title:@"- -" titleColor:UIColorFromRGB(0x333333) font:20 textAlignment:1];
        //    calorieL.transform = CGAffineTransformScale(runTimerL.transform, 0.9, 0.9);
        matchSpeed.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20];
        _maSpL = matchSpeed;
        [runView addSubview:matchSpeed];
        
        
        UILabel * calorieL = [ReduceCode creatLabelWithFrame:CGRectMake(kScreenWidth/4*3, kMaxY(runTimerL.frame)-20, kScreenWidth/4, 20) title:@"- -" titleColor:UIColorFromRGB(0x333333) font:20 textAlignment:1];
        //    runNumL.transform = CGAffineTransformScale(runTimerL.transform, 0.9, 0.9);
        calorieL.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20];
        _kcalL = calorieL;
        [runView addSubview:calorieL];
        
        //    CGFloat weight =(kScreenWidth-90*3)/4;
        
        NSArray * text = @[@"Min/Km",@"Time",@"Kcal"];
        UILabel * l= nil;
        for (int U = 0; U<3; U++) {
            
            UILabel * runNumL = [ReduceCode creatLabelWithFrame:CGRectMake(kScreenWidth/4*U, kMaxY(runTimerL.frame)+12, kScreenWidth/4, 14) title:text[U] titleColor:UIColorFromRGB(0x000000) font:14 textAlignment:NSTextAlignmentCenter];
            if (U == 1) {
                runNumL.width = kScreenWidth/2;
            }
            if (U == 2) {
                runNumL.x = kScreenWidth/4*3;
            }
            l = runNumL;
            [runView addSubview:runNumL];
        }
        
        
        CGFloat weight = (kScreenWidth-105)/2;
        
        UIButton * endBtn = [UIButton creatBtnWithFrame:CGRectMake(weight, runView.height-105-24, 105, 105) btnTitle:@"结束" btnTitleColor:kBlackColor btnTitleFont:0 btnSetImage:nil btnBackgroundColor:nil backgroundImage:IMAGE(@"violet") isCorner:YES action:^(UIButton *button) {
            [self finishTimer];
        }];
   
        endBtn.alpha = 0;
        [endBtn setBackgroundImage:IMAGE(@"gray-violet") forState:UIControlStateHighlighted];
        _enB = endBtn;
        [runView addSubview:endBtn];
        
        
        UIButton * beginBtn = [UIButton creatBtnWithFrame:CGRectMake(weight, runView.height-105-24, 105, 105) btnTitle:@"暂停" btnTitleColor:kBlackColor btnTitleFont:0 btnSetImage:nil btnBackgroundColor:nil backgroundImage:IMAGE(@"red") isCorner:YES action:^(UIButton *button) {
            
            isRun = !isRun;
            
            [self beginaDidAnimation];
            [self suspendTimer];
            
        }];
        
        [beginBtn setBackgroundImage:IMAGE(@"gray-red") forState:UIControlStateHighlighted];
        
        _beB = beginBtn;
        [runView addSubview:beginBtn];
        
    

    
    
}

#pragma mark--  开始并做动画
- (void)beginaDidAnimation {
    
    CGFloat weight = ((kScreenWidth-105*2)/3+105)/2;

    POPSpringAnimation *begin = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    begin.springSpeed = 20;
    
    POPSpringAnimation *suspend = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    suspend.springSpeed = 20;
    
    //创建一个POPBasicAnimation动画 透明度的变化
    POPBasicAnimation *basicAnimation=[POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
//    basicAnimation.duration=2; //设置动画的间隔时间 默认是0.4秒
//    basicAnimation.repeatCount=HUGE_VALF; //重复次数 HUGE_VALF设置为无限次重复
    
    if (isRun) {
        begin.toValue   = @(self.centerX);
        suspend.toValue = @(self.centerX);
        begin.springBounciness = 0;
        suspend.springBounciness = 0;
        basicAnimation.fromValue=@(1);
        basicAnimation.toValue=@(0);
        [_beB setTitle:@"暂停" forState:UIControlStateNormal];
    }else {
        begin.toValue = @(_beB.centerX+weight);
        suspend.toValue = @(_enB.centerX-weight);
        begin.springBounciness = 12;
        suspend.springBounciness = 12;
        
        basicAnimation.fromValue=@(0);
        basicAnimation.toValue=@(1);
        
         [_beB setTitle:@"继续" forState:UIControlStateNormal];
    }
    
    
    [_beB.layer  pop_addAnimation:begin forKey:@"beginGo"];
    
    [_enB.layer pop_addAnimation:suspend forKey:@"suspendanim"];
    
    
     [_enB pop_addAnimation:basicAnimation forKey:@"myViewAnimation"];
}

#pragma mark -- 计时开始
- (void)timerBegin {
    
    if (!_timer) {
       _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
    }
 
}


- (void)startTimer {
    S++;
    if (S == 60) {
        min++;
        S = 0;
        if (min == 60) {
            hour ++;
            min = 0;
        }
    }
    _runTimerL.text = [NSString stringWithFormat:@"%02li:%02li:%02li",hour,min,S];
    
    NSLog(@"%@",_runTimerL.text);
    
}

#pragma mark -- 计时暂停

- (void)suspendTimer {
    
    if (!isRun) {
        //暂停
        [_timer setFireDate:[NSDate distantFuture]];
    }else {
        //继续
        [_timer  setFireDate:[NSDate distantPast]];
    }
    
    if (_newBlock) {
        _newBlock(@"next",isRun);
    }
}


#pragma mark -- 计时停止

- (void)finishTimer {

    if (_newBlock) {
        _newBlock(@"stop",nil);
    }
    
}




@end
